package com.whmskj.xjlhsc.stationmachine.service.imp;

import com.whmskj.xjlhsc.stationmachine.mapper.YieldMapper;
import com.whmskj.xjlhsc.stationmachine.pojo.entity.YieldTimeVO;
import com.whmskj.xjlhsc.stationmachine.pojo.entity.YieldVarietyVO;
import com.whmskj.xjlhsc.stationmachine.pojo.vo.YieldDataVO;
import com.whmskj.xjlhsc.stationmachine.service.IYieldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author jiaruchun
 * @date 2025-09-17 19:10
 */
@Service
public class YieldServiceImpl implements IYieldService {
    @Autowired
    private YieldMapper yieldMapper;

    @Override
    public YieldDataVO reYieldData(String process) {
        YieldDataVO yieldDataVO = new YieldDataVO();
        //日-----
//        String process = yieldDataDTO.getProcess();
        if(process == null || process.isEmpty()){
            process = "细纱";
        }
        List<YieldVarietyVO> yieldVarietyList = yieldMapper.getYieldVarietyVOList(process);
        if (yieldVarietyList != null && !yieldVarietyList.isEmpty()) {
            //保留两位小数
            for (YieldVarietyVO yieldVarietyVO : yieldVarietyList) {
                yieldVarietyVO.setYield(Math.round(yieldVarietyVO.getYield() * 100.0) / 100.0f);
            }
        }
        yieldDataVO.setYieldVarietyListDay(yieldVarietyList);
        List<YieldTimeVO> yieldTimeVOList = yieldMapper.getYieldTimeVO(process);
        if (yieldTimeVOList != null && !yieldTimeVOList.isEmpty()) {
            //保留两位小数
            for (YieldTimeVO yieldTimeVO : yieldTimeVOList) {
                yieldTimeVO.setYield(Math.round(yieldTimeVO.getYield() * 100.0) / 100.0f);
            }
            //取集合最后一个元素
            yieldDataVO.setYield(yieldTimeVOList.get(yieldTimeVOList.size() - 1).getYield());
        }else {
            yieldDataVO.setYield(0f);
        }
        yieldDataVO.setTimeListDay(yieldTimeVOList);
        //月-----
        List<YieldVarietyVO> yieldVarietyListMonth = yieldMapper.getYieldVarietyVOListMonth(process);
        if (yieldVarietyListMonth != null && !yieldVarietyListMonth.isEmpty()) {
            //保留两位小数
            for (YieldVarietyVO yieldVarietyVO : yieldVarietyListMonth) {
                yieldVarietyVO.setYield(Math.round(yieldVarietyVO.getYield() * 100.0) / 100.0f);
            }
        }
        yieldDataVO.setYieldVarietyListMonth(yieldVarietyListMonth);
        List<YieldTimeVO> yieldTimeVOMonth = yieldMapper.getYieldTimeVOMonth(process);
        if (yieldTimeVOMonth != null && !yieldTimeVOMonth.isEmpty()) {
            //保留两位小数
            for (YieldTimeVO yieldTimeVO : yieldTimeVOMonth) {
                yieldTimeVO.setYield(Math.round(yieldTimeVO.getYield() * 100.0) / 100.0f);
            }
            //取集合最后一个元素
            yieldDataVO.setMonthYield(yieldTimeVOMonth.get(yieldTimeVOMonth.size() - 1).getYield());
        }else {
            yieldDataVO.setMonthYield(0f);
        }
        yieldDataVO.setTimeListMonth(yieldTimeVOMonth);
        //年----------
        List<YieldVarietyVO> yieldVarietyListYear = yieldMapper.getYieldVarietyVOListYear(process);
        if (yieldVarietyListYear != null && !yieldVarietyListYear.isEmpty()) {
            //保留两位小数
            for (YieldVarietyVO yieldVarietyVO : yieldVarietyListYear) {
                yieldVarietyVO.setYield(Math.round(yieldVarietyVO.getYield() * 100.0) / 100.0f);
            }
        }
        yieldDataVO.setYieldVarietyListYear(yieldVarietyListYear);
        List<YieldTimeVO> yieldTimeVOYear = yieldMapper.getYieldTimeVOYear(process);
        if (yieldTimeVOYear != null && !yieldTimeVOYear.isEmpty()) {
            //保留两位小数
            for (YieldTimeVO yieldTimeVO : yieldTimeVOYear) {
                yieldTimeVO.setYield(Math.round(yieldTimeVO.getYield() * 100.0) / 100.0f);
            }
            //取集合最后一个元素
            yieldDataVO.setYearYield(yieldTimeVOYear.get(yieldTimeVOYear.size() - 1).getYield());
        }else {
            yieldDataVO.setYearYield(0f);
        }
        yieldDataVO.setTimeListYear(yieldTimeVOYear);
        return yieldDataVO;
    }
}
